package com.lc.pianostudent.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.PianoTimeAdapter;
import com.lc.pianostudent.bean.PianoBean;
import com.lc.pianostudent.conn.PostRankingOldStudy;
import com.lc.pianostudent.conn.PostRankingStudy;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class PianoListOldActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.piano_time,isClick = true) private TextView piano_time_text;
    @BoundView(value = R.id.serious_index,isClick = true) private TextView serious_index_text;

    private int width = 720;
    private int height = 1280;



    @BoundView(R.id.piano_list_recycleview) private XRecyclerView recyclerView;
    private PianoTimeAdapter pianoTimeAdapter;

    public List<PianoBean> indexList = new ArrayList<>();
//    private PianoIndexAdapter pianoIndexAdapter;
    private int type_text = 1;

    private  PostRankingOldStudy.PostRankingOldStudyInfo currentInfo;
    private PostRankingOldStudy postRankingOldStudy= new PostRankingOldStudy(new AsyCallBack<PostRankingOldStudy.PostRankingOldStudyInfo>() {


        @Override
        public void onSuccess(String toast, int type, Object o, PostRankingOldStudy.PostRankingOldStudyInfo postRankingOldStudyInfo) throws Exception {
                        if(type == 0){
                indexList.clear();
            }
            currentInfo = postRankingOldStudyInfo;

//            Log.e("====",postRankingOldStudyInfo.list.get(0).getTime());
            indexList.addAll(postRankingOldStudyInfo.list);
            pianoTimeAdapter.notifyDataSetChanged();

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piano_old_list);

        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("练琴排行榜");

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）
        height = dm.heightPixels;       // 屏幕高度（像素）
        Log.e("=====jin",width + "--width");
        Log.e("=====jin",height + "--height");

        piano_time_text.setBackgroundResource(R.mipmap.ph_button_select);
        serious_index_text.setBackgroundResource(R.mipmap.ph_button_default);
        piano_time_text.getLayoutParams().height =  width/9;
        piano_time_text.getLayoutParams().width = (int) (width/4.16);

        serious_index_text.getLayoutParams().height = (int) (width/10.75);
        serious_index_text.getLayoutParams().width = (int) (width/4.19);

        piano_time_text.setTextColor(Color.parseColor("#ffffff"));
        serious_index_text.setTextColor(Color.parseColor("#0d4986"));

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        pianoTimeAdapter = new PianoTimeAdapter(this, indexList);
        recyclerView.setAdapter(pianoTimeAdapter);

        postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
        postRankingOldStudy.type = "1";
        postRankingOldStudy.teacherid =BaseApplication.BasePreferences.readTeacherId();
        postRankingOldStudy.page = "1";
        postRankingOldStudy.execute();

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if(type_text == 1){
                    postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                    postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();
                    postRankingOldStudy.type = "1";
//                    postQuestionRanking.teacherid = "3";
                    postRankingOldStudy.page = "1";
                    postRankingOldStudy.execute(context,false,0);
                }else if(type_text == 2){
                    postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                    postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();
                    postRankingOldStudy.type = "2";
//                    postQuestionRanking.teacherid = "3";
                    postRankingOldStudy.page = "1";
                    postRankingOldStudy.execute(context,false,0);
                }

            }

            @Override
            public void onLoadMore() {
                if(type_text == 1){
//                    Log.e("=====page",currentInfo.per_page+"==page");
                    if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){

                        postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                        postRankingOldStudy.type = "1";
                        postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();
//                        postQuestionRanking.teacherid = "3";
                        postRankingOldStudy.page = currentInfo.current_page+1+"";
                        postRankingOldStudy.execute(context,false,1);
                    }else {
                        Log.e("=====page","------22");
                        UtilToast.show("暂无更多数据");
                        recyclerView.refreshComplete();
                        recyclerView.loadMoreComplete();
                    }
                }else if(type_text == 2){
//                    Log.e("=====page",currentInfo.per_page+"==page");
                    if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){

                        postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                        postRankingOldStudy.type = "2";
                        postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();

//                        postQuestionRanking.teacherid = "3";
                        postRankingOldStudy.page = currentInfo.current_page+1+"";
                        postRankingOldStudy.execute(context,false,1);
                    }else {
                        Log.e("=====page","------22");
                        UtilToast.show("暂无更多数据");
                        recyclerView.refreshComplete();
                        recyclerView.loadMoreComplete();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.piano_time:
                type_text = 1;
                piano_time_text.setBackgroundResource(R.mipmap.ph_button_select);
                serious_index_text.setBackgroundResource(R.mipmap.ph_button_default);
                piano_time_text.getLayoutParams().height =  width/9;
                piano_time_text.getLayoutParams().width = (int) (width/4.16);

                serious_index_text.getLayoutParams().height = (int) (width/10.75);
                serious_index_text.getLayoutParams().width = (int) (width/4.19);

                piano_time_text.setTextColor(Color.parseColor("#ffffff"));
                serious_index_text.setTextColor(Color.parseColor("#0d4986"));

                postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();
                postRankingOldStudy.type = "1";
                postRankingOldStudy.page = "1";
                postRankingOldStudy.execute(context,false,0);
                break;

            case R.id.serious_index:
                type_text = 2;
                piano_time_text.setBackgroundResource(R.mipmap.ph_button_default);
                serious_index_text.setBackgroundResource(R.mipmap.ph_button_select);
                piano_time_text.getLayoutParams().height = (int) (width/10.75);
                piano_time_text.getLayoutParams().width = (int) (width/4.16);

                serious_index_text.getLayoutParams().height = width/9;
                serious_index_text.getLayoutParams().width = (int) (width/4.19);

                piano_time_text.setTextColor(Color.parseColor("#0d4986"));
                serious_index_text.setTextColor(Color.parseColor("#ffffff"));

                postRankingOldStudy.user_id = BaseApplication.BasePreferences.readUID();
                postRankingOldStudy.teacherid = BaseApplication.BasePreferences.readTeacherId();
                postRankingOldStudy.type = "2";
                postRankingOldStudy.page = "1";
                postRankingOldStudy.execute(context,false,0);
                break;



        }
    }

}
